# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=openjdk11-loongarch
pkgver=11.0.24_p8
pkgrel=0
_majorver=${pkgver%%.*}
_pkgver=${pkgver/_p/+}-ls-ga
pkgdesc="Loongarch port of OpenJDK $_majorver"
url="https://github.com/loongson/jdk${_majorver}u"
arch="loongarch64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
options="sover-namecheck !check" # tests get stuck forever on the builders
makedepends_build="
	openjdk$_majorver-bootstrap
	autoconf
	bash
	gawk
	grep
	make
	zip
	zlib-dev
	"
makedepends_host="
	alsa-lib-dev
	cups-dev
	elfutils-dev
	fontconfig-dev
	freetype-dev
	giflib-dev
	lcms2-dev
	libffi-dev
	libjpeg-turbo-dev
	libx11-dev
	libxext-dev
	libxrandr-dev
	libxrender-dev
	libxt-dev
	libxtst-dev
	linux-headers
	zlib-dev
	"
[ "$CBUILD" = "$CTARGET" ] && makedepends="
	$makedepends_build
	$makedepends_host
	java-cacerts
	java-common
	"
subpackages="
	$pkgname-demos::noarch
	$pkgname-doc
	$pkgname-jmods::noarch
	$pkgname-jre
	$pkgname-jre-headless:jre_headless
	$pkgname-jdk
	"
source="$pkgname-$_pkgver.tar.gz::https://github.com/loongson/jdk${_majorver}u/archive/refs/tags/jdk-$_pkgver.tar.gz
	JDK-8267908.patch
	lfs64.patch
	loongarch-fpu_control.patch
	"
builddir="$srcdir/jdk${_majorver}u-jdk-${_pkgver/+/-}"

case "$CARCH" in
loongarch64)
	_jvm_variants=server
	provides="openjdk$_majorver-bootstrap openjdk$_majorver"
	replaces="openjdk$_majorver"
	provider_priority=$((_majorver + 1))
	;;
esac

_java_home="/usr/lib/jvm/java-$_majorver-openjdk"

ldpath="$_java_home/lib:$_java_home/lib/jli:$_java_home/lib/$_jvm_variants"
sonameprefix="$pkgname:"

# enable running the JTReg tests in check?
# see comment in that function for explanation
_run_jtreg=${_run_jtreg:-0}
if [ $_run_jtreg -ne 0 ]; then
	makedepends="$makedepends java-jtreg"
	checkdepends="$checkdepends font-freefont xvfb-run"
fi

prepare() {
	default_prepare

	# update autoconf files to detect alpine
	update_config_sub

	case "$CARCH" in
	loongarch64)
		;;
	*)
		error "Please use community/openjdk11"
		return 1
		;;
	esac
}

build() {
	if [ $_run_jtreg -ne 0 ]; then
		_with_jtreg="--with-jtreg=/usr/share/java/jtreg"
	else
		_with_jtreg="--with-jtreg=no"
	fi

	if [ -n "$USE_CCACHE" ]; then
		# workaround ccache being disallowed
		export PATH="/usr/bin:/bin:/sbin:/usr/sbin"
		local ccache="--enable-ccache"
	fi

	if [ -z "$JOBS" ]; then
		export JOBS=$(printf '%s\n' "$MAKEFLAGS" | sed -n -e 's/.*-j\([0-9]\+\).*/\1/p')
	fi

	# we want to build hotspot with better optimisations; it's set to this
	# (prepended) anyway, and it's huge
	export CFLAGS="$CFLAGS -O3"
	export CXXFLAGS="$CXXFLAGS -O3"

	# CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
	# in the output of ./configure unless used like such:
	#  --with-extra-cflags="$CFLAGS"
	#  --with-extra-cxxflags="$CXXFLAGS"
	#  --with-extra-ldflags="$LDFLAGS"
	# See also paragraph "Configure Control Variables" from "common/doc/building.md"
	# shellcheck disable=2097 disable=2098
	CFLAGS='' CXXFLAGS='' LDFLAGS='' \
		bash ./configure \
		--openjdk-target=$CHOST \
		--prefix="$_java_home" \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-extra-cflags="$CFLAGS" \
		--with-extra-cxxflags="$CXXFLAGS" \
		--with-extra-ldflags="$LDFLAGS" \
		--with-zlib=system \
		--with-libjpeg=system \
		--with-giflib=system \
		--with-libpng=system \
		--with-lcms=system \
		--with-jobs=${JOBS:-4} \
		--with-test-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		--with-build-jdk="$_java_home" \
		$ccache \
		$_with_jtreg \
		--disable-warnings-as-errors \
		--disable-precompiled-headers \
		--enable-dtrace=no \
		--with-jvm-variants=$_jvm_variants \
		--with-debug-level=release \
		--with-version-pre= \
		--with-version-build="${pkgver##*p}" \
		--with-version-opt="${_pkgver#*-}-alpine-r$pkgrel" \
		--with-vendor-name="Alpine" \
		--with-vendor-url="https://alpinelinux.org/" \
		--with-vendor-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues" \
		--with-vendor-vm-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues"

	MAKEFLAGS='' make images
}

check() {
	# run the gtest unittest suites
	# they don't take long, DO NOT DISABLE THEM!
	# override make variable, disable -Werror
	MAKEFLAGS='' make test-hotspot-gtest

	# The jtreg tests take very, very long to finish and show some failures (9 - 12 on my machine, varying between runs)
	# I think these are not critical and can be safely ignored.
	# As the tests take too long, they are disabled by default.
	# When updating this aport please let them run at least once on your machine to see if the failure count changes.
	if [ $_run_jtreg -ne 0 ]; then
		_logfile=$( mktemp -p "$builddir" )
		MAKEFLAGS='' xvfb-run make \
			run-test-tier1 \
			run-test-tier2 \
			run-test-tier3 \
			| tee "$_logfile"
		msg "---------------------------------------"
		msg "The build log can be found at $_logfile"
		# abort the build so you may take a look at the logfile
		false
		return 1
	fi
}

package() {
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	install -Dvm644 ASSEMBLY_EXCEPTION LICENSE README.md \
		-t "$pkgdir"/$_java_home/

	cd build/linux-*-$_jvm_variants-release/images/
	cp -a jdk/* "$pkgdir"/$_java_home/
	rm -v "$pkgdir"/$_java_home/lib/src.zip

	# symlink to shared java cacerts store
	rm -v "$pkgdir"/$_java_home/lib/security/cacerts
	ln -sv /etc/ssl/certs/java/cacerts \
		"$pkgdir"/$_java_home/lib/security/cacerts

	# symlink for java-common to work (expects jre in $_java_home/jre)
	ln -sv . "$pkgdir"/$_java_home/jre
}

jmods() {
	pkgdesc="$pkgdesc (jmods)"
	depends=

	provides="openjdk$_majorver-jmods"
	replaces="openjdk$_majorver-jmods"

	amove $_java_home/jmods
}

jre() {
	pkgdesc="$pkgdesc (JRE)"
	depends="$pkgname-jre-headless=$pkgver-r$pkgrel"

	provides="java-jre openjdk$_majorver-jre"
	replaces="openjdk$_majorver-jre"

	amove \
		$_java_home/lib/libawt_xawt.so \
		$_java_home/lib/libfontmanager.so \
		$_java_home/lib/libjavajpeg.so \
		$_java_home/lib/libjawt.so \
		$_java_home/lib/libjsound.so \
		$_java_home/lib/liblcms.so \
		$_java_home/lib/libsplashscreen.so
}

jre_headless() {
	pkgdesc="$pkgdesc (JRE headless)"
	depends="java-common java-cacerts"

	provides="java-jre-headless openjdk$_majorver-jre-headless"
	replaces="openjdk$_majorver-jre-headless"

	amove \
		$_java_home/bin/java \
		$_java_home/bin/jfr \
		$_java_home/bin/jjs \
		$_java_home/bin/jrunscript \
		$_java_home/bin/keytool \
		$_java_home/bin/pack200 \
		$_java_home/bin/rmid \
		$_java_home/bin/rmiregistry \
		$_java_home/bin/unpack200 \
		$_java_home/conf \
		$_java_home/jre \
		$_java_home/release

	# Rest of the lib subdir (which were not taken by -jre subpkg).
	amove $_java_home/lib

	# ct.sym should stay in -jdk
	mkdir -p "$pkgdir"/$_java_home/lib
	mv "$subpkgdir"/$_java_home/lib/ct.sym "$pkgdir"/$_java_home/lib
}

doc() {
	default_doc

	amove $_java_home/man
}

demos() {
	pkgdesc="$pkgdesc (demos)"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	provides="openjdk$_majorver-demos"
	replaces="openjdk$_majorver-demos"

	amove $_java_home/demo
}

jdk() {
	pkgdesc="$pkgdesc (JDK) ($_jvm_variants variant)"
	depends="$pkgname-jre=$pkgver-r$pkgrel $pkgname-jmods=$pkgver-r$pkgrel"

	provides="java-jdk openjdk$_majorver-jdk"
	replaces="openjdk$_majorver-jdk"

	amove \
		$_java_home/bin \
		$_java_home/lib \
		$_java_home/include
}

sha512sums="
8c3ccbd8ba57cd97684dd4cfa189a937c4ee80e113f3ea54c9fe4ae59598fde6fef4acc5db00fd891246f5c466277ca80195bb79dfe042d834209d4251146e7f  openjdk11-loongarch-11.0.24+8-ls-ga.tar.gz
b0963e5b6dc4d6cec0670827e0a0691d65e44587a8912ac9110aeb36d7f2d07a8afe9e155ea1568fe1534c09ef3277aeca8a66bbf155354b5cdc6e2b9636b5b4  JDK-8267908.patch
438c6c4c760b7aece90a8e09d5edce842286071195cffe8d417208f3dfdef0953eb68fb8282d44cbd7ac8eeaee637cdb040c06ffa5381c36f47ffd48b5e6d938  lfs64.patch
674229c4192ea0d7adc263934f7a4be662b064b7622526d32a187319dcff0fdcd0ba63a7640167604dc32646894071828c6c24dd9914fb0e0007bd2de4af197d  loongarch-fpu_control.patch
"
